package org.openkoala.auth.application.vo;

import java.io.Serializable;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.dayatang.utils.DateUtils;
import org.openkoala.koala.auth.core.domain.Resource;

public class ResourceVO extends PartyVO implements Serializable {

	private static final long serialVersionUID = 1248753171821130218L;

	private String identifier;
	private String level;
	private String icon;
	private String typeId;
	private String typeName;
	private String name;
	private String text;
	private Long parentId;
	private boolean ischecked;
	private boolean isleaf;
	private boolean isvalid;
	private String desc;
	private String menuType;
	private List<ResourceVO> children = new ArrayList<ResourceVO>();

	public ResourceVO() {

	}

	public ResourceVO(String identifier, String typeId, String name, String desc) {
		this.identifier = identifier;
		this.typeId = typeId;
		this.name = name;
		this.desc = desc;
	}

	public boolean isIschecked() {
		return ischecked;
	}

	public void setIschecked(boolean ischecked) {
		this.ischecked = ischecked;
	}

	public boolean isIsvalid() {
		return isvalid;
	}

	public void setIsvalid(boolean isvalid) {
		this.isvalid = isvalid;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getTypeId() {
		return typeId;
	}

	public void setTypeId(String typeId) {
		this.typeId = typeId;
	}

	public String getTypeName() {
		return typeName;
	}

	public void setTypeName(String typeName) {
		this.typeName = typeName;
	}

	public String getIdentifier() {
		return identifier;
	}

	public void setIdentifier(String identifier) {
		this.identifier = identifier;
	}

	public String getLevel() {
		return level;
	}

	public void setLevel(String level) {
		this.level = level;
	}

	public String getIcon() {
		return icon;
	}

	public void setIcon(String icon) {
		this.icon = icon;
	}

	public String getText() {
		return text;
	}

	public void setText(String text) {
		this.text = text;
	}

	public Long getParentId() {
		return parentId;
	}

	public void setParentId(Long parentId) {
		this.parentId = parentId;
	}

	public boolean isIsleaf() {
		return isleaf;
	}

	public void setIsleaf(boolean isleaf) {
		this.isleaf = isleaf;
	}

	public List<ResourceVO> getChildren() {
		return children;
	}

	public void setChildren(List<ResourceVO> children) {
		this.children = children;
	}

	public String getDesc() {
		return desc;
	}

	public void setDesc(String desc) {
		this.desc = desc;
	}

	public String getMenuType() {
		return menuType;
	}

	public void setMenuType(String menuType) {
		this.menuType = menuType;
	}

	public ResourceVO(Long id, String desc, int version, String icon, String level, String identifier, Boolean isvalid, String name, String text,
			Integer sortOrder, String serialNumber, Date abolishDate, Date createDate, Long menuType) {
		DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		this.setId(id);
		this.setDesc(desc);
		this.setVersion(version);
		this.setIcon(icon);
		this.setLevel(level == null ? "1" : level);
		this.setIdentifier(identifier);
		this.setIsvalid(isvalid);
		this.setName(name);
		this.setText(text);
		this.setSortOrder(sortOrder);
		this.setSerialNumber(serialNumber);
		this.setAbolishDate(formatter.format(abolishDate));
		this.setCreateDate(formatter.format(createDate));
		this.setMenuType(String.valueOf(menuType));
	}

	public ResourceVO(Long parentId, Long id, String desc, int version, String icon, String level, String identifier, Boolean isvalid, String name,
			String text, Integer sortOrder, String serialNumber, Date abolishDate, Date createDate, Long menuType) {
		this(id, desc, version, icon, level, identifier, isvalid, name, text, sortOrder, serialNumber, abolishDate, createDate, menuType);
		this.setParentId(parentId);
	}

	public void domain2Vo(Resource resource) {
		DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		this.setId(resource.getId());
		this.setDesc(resource.getDesc());
		this.setVersion(resource.getVersion());
		this.setIcon(resource.getMenuIcon());
		this.setLevel(resource.getLevel() == null ? "1" : resource.getLevel().toString());
		this.setIdentifier(resource.getIdentifier());
		this.setIsvalid(resource.isValid());
		this.setName(resource.getName());
		this.setText(resource.getName());
		this.setSortOrder(resource.getSortOrder());
		this.setSerialNumber(resource.getSerialNumber());
		this.setAbolishDate(formatter.format(resource.getAbolishDate()));
		this.setCreateDate(formatter.format(resource.getCreateDate()));
	}

	public void vo2Domain(Resource resource) {
		resource.setId(this.getId());
		resource.setVersion(this.getVersion());
		resource.setMenuIcon(this.getIcon());
		resource.setName(this.getName());
		resource.setLevel(this.getLevel());
		resource.setName(this.getName());
		resource.setIdentifier(this.getIdentifier());
		resource.setDesc(this.getDesc());
		resource.setValid(true);
		resource.setAbolishDate(DateUtils.MAX_DATE);
		resource.setCreateDate(new Date());
		resource.setSerialNumber(this.getSerialNumber() == null || "".equals(this.getSerialNumber()) ? "0" : this.getSerialNumber());
		resource.setSortOrder(this.getSortOrder());
	}

	@Override
	public String toString() {
		return "ResourceVO [name=" + name + ", ischecked=" + ischecked + ", menuType=" + menuType + ", children=" + children + "]";
	}

	public void addChild(ResourceVO childVO) {
		this.children.add(childVO);
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = super.hashCode();
		result = prime * result + ((identifier == null) ? 0 : identifier.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (!super.equals(obj))
			return false;
		if (getClass() != obj.getClass())
			return false;
		ResourceVO other = (ResourceVO) obj;
		if (identifier == null) {
			if (other.identifier != null)
				return false;
		} else if (!identifier.equals(other.identifier))
			return false;
		return true;
	}

}
